package flc.ast.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.FileTransferActivity;
import flc.ast.adapter.SelAudioAdapter;
import flc.ast.bean.SelectFileBean;
import flc.ast.databinding.FragmentAudioBinding;
import flc.ast.utils.SendFileManager;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.media.MediaLoader;
import stark.common.basic.media.bean.AudioBean;
import stark.common.basic.utils.RxUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AudioFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AudioFragment extends BaseNoModelFragment<FragmentAudioBinding> {

    private SendFileManager mInstance;
    private SelAudioAdapter selAudioAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_audio;
    }

    @Override
    protected void initView() {
        mInstance = SendFileManager.getInstance();

        mDataBinding.rvAudioList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        selAudioAdapter = new SelAudioAdapter();
        mDataBinding.rvAudioList.setAdapter(selAudioAdapter);
        selAudioAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        selAudioAdapter.getItem(position).setSelected(!selAudioAdapter.getItem(position).isSelected());
        selAudioAdapter.notifyDataSetChanged();
        boolean hasExist = false;
        int tmpPosition = -1;
        for (SelectFileBean videoCollectionBean : mInstance.getSelectFileBeans()) {
            tmpPosition++;
            if (selAudioAdapter.getItem(position).getUri().equals(videoCollectionBean.getPath())) {
                hasExist = true;
                break;
            }
        }
        if (hasExist) {
            mInstance.deleteSelectFile(tmpPosition);
        } else {
            SelectFileBean bean = new SelectFileBean();
            bean.setType(3);
            bean.setName(selAudioAdapter.getItem(position).getName());
            bean.setPath(selAudioAdapter.getItem(position).getUri());
            bean.setSize(selAudioAdapter.getItem(position).getSize());
            mInstance.addSelectFile(bean);
        }
        ((FileTransferActivity) mContext).refreshDataCount(mInstance);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<AudioBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<AudioBean>> emitter) {
                List<AudioBean> audioBeans = MediaLoader.loadAudio();
                emitter.onNext(audioBeans);
            }

            @Override
            public void accept(List<AudioBean> audioBeanList) {
                if (audioBeanList != null && audioBeanList.size() != 0) {
                    selAudioAdapter.setList(audioBeanList);
                    mDataBinding.rvAudioList.setVisibility(View.VISIBLE);
                    mDataBinding.tvAudioNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvAudioList.setVisibility(View.GONE);
                    mDataBinding.tvAudioNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}